package ee.propaganda.gparrot.server;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import ee.propaganda.gparrot.client.app.ajax.AuthService;
import ee.propaganda.gparrot.client.app.ajax.NotLoggedInException;
import ee.propaganda.gparrot.client.model.LoginInfo;
import ee.propaganda.gparrot.client.model.ServiceInfo.ServiceType;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class AuthServiceImpl extends RemoteServiceServlet implements AuthService { 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LoginInfo login(String requestUri) {
		LoginInfo loginInfo = new LoginInfo();
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
	
		if (user != null) {
			loginInfo.setLoggedIn(true);
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
			loginInfo.setNickname(user.getNickname());
			loginInfo.setEmailAddress(user.getEmail());
		} else {
			loginInfo.setLoggedIn(false);
			loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		}
		return loginInfo;
	}

	@Override
	public String getAccess(LoginInfo loginInfo, ServiceType serviceType) throws NotLoggedInException {
		// TODO Auto-generated method stub
		return "test";
	}
}
